function format(time, format){
	    var t = new Date(time);
	    var tf = function(i){return (i < 10 ? '0' : '') + i};
	    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
	        switch(a){
	            case 'yyyy':
	                return tf(t.getFullYear());
	                break;
	            case 'MM':
	                return tf(t.getMonth() + 1);
	                break;
	            case 'mm':
	                return tf(t.getMinutes());
	                break;
	            case 'dd':
	                return tf(t.getDate());
	                break;
	            case 'HH':
	                return tf(t.getHours());
	                break;
	            case 'ss':
	                return tf(t.getSeconds());
	                break;
	        }
	    })
	}
    
    function initTable() {
		var table = $('#masterTable2').dataTable({			        
			 "columnDefs":[{
				 searchable:true
	       }],             
			"oLanguage" : { // 汉化
				"sProcessing" : "正在加载数据...",
				"sLengthMenu" : "_MENU_记录/页 ",
				"sZeroRecords" : "没有您要搜索的内容",
				"sInfo" : "_START_ 至 _END_  ，共 _TOTAL_ 项",
				"sInfoEmpty" : "记录数为0",
				"sInfoFiltered" : "(全部记录数 _MAX_  条)",
				"sInfoPostFix" : "",
				"sSearch" : "查找&nbsp;",
				"sUrl" : "",
				"oPaginate" : {
					"sFirst" : "第一页",
					"sPrevious" : " 上一页 ",
					"sNext" : " 下一页 ",
					"sLast" : " 最后一页 "
				}
			}
		});
	}
    
    
	 $("#QRform").validate({
			rules : {
				qrcodeId : {
					required : true
				}
			},
			messages : {
				qrcodeId : {
					required : "请输入二维码编号"
				}
			}
		});
	
    
    function qrcodeSearch(){
		if (!$("#QRform").valid()){
			return;
		}
    	var qrcodeType = $("#qrcodeType option:selected").val();    	
    	if(qrcodeType != 0 && qrcodeType != 1){
			return;
		}
    	var index = layer.load(1, {
			shadeClose : false,
			shade: [0.5,'#D3D3D3'] //0.1透明度的白色背景
		});
    	$('#child').css('display','none');
    	$('#master').css('display','none');
    	$('#master2').css('display','none');
    	$("#btn").attr({"disabled":"disabled"});
    	
    	
		$.ajax({
			cache : true,
			type : "POST",
			url : "qrcodeSearch",
			data : $('#QRform').serialize(),// 你的formid
			error : function(request) {
				layer.closeAll();
				$('#alertModal').modal('show');
				document.getElementById("alertMessage").innerHTML = "Connection error！";
				$("#btn").removeAttr("disabled");
			},
			success : function(data) {
				var msg = data.msg;
				layer.closeAll();
				if(msg != null){//查询失败
					$('#alertModal').modal('show');
					document.getElementById("alertMessage").innerHTML = msg;
					$("#btn").removeAttr("disabled");
					return ;
				}
				if(qrcodeType == 0){//查询子码
					var date = data.master.send_time==null?"":format(data.master.send_time,"yyyy-MM-dd HH:mm:ss");
					var id = data.master.id==null?"":data.master.id;
					var area_code = data.master.area_code==null?"":data.master.area_code;
					var str = '<td>'+id+'</td><td>'+area_code+'</td><td>'+date+'</td>';
					$("#childTable tbody tr").html(str);
					
					$('#child').css('display','block');
					$("#btn").removeAttr("disabled");
				}else{//查主码
					$("#master2").html("");
					var id = data.master.id==null?"":data.master.id;
					var area_code = data.master.area_code==null?"":data.master.area_code;
					var date = data.master.send_time==null?"":format(data.master.send_time,"yyyy-MM-dd HH:mm:ss");
					var str = '<td>'+id+'</td><td>'+area_code+'</td><td>'+date+'</td>';
					$("#masterTable1 tbody tr").html(str);
					
					var bodyStr = '<table class="table table-striped table-bordered table-hover dataTables-example text-center" id="masterTable2"><thead><tr><th class="text-center">序号</th><th class="text-center">子码编号</th></tr></thead><tbody>';
                    
                    var list = data.childCodeList;
                    for(var i=0;i<list.length;i++){
                    	bodyStr += '<tr><td>'+(i+1)+'</td><td>'+list[i]+'</td></tr>';
                    }
                    bodyStr += '</tbody></table>';
                    
                    $("#master2").html(bodyStr);                     
        			initTable();
        			
        			
					$('#master').css('display','block');
					$('#master2').css('display','block');
					$("#btn").removeAttr("disabled");	
				}								
			}
		})
	}